﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DriveBeamEditor.Controls.BeamNG.Forms
{
    public partial class QuickTipBox : Form
    {
        #region Fields

        private DateTime _lastTime;

        #endregion

        #region Properties

        public string Description
        {
            get { return labelDescription.Text; }
            set { labelDescription.Text = value; }
        }

        #endregion

        #region CTORs

        private QuickTipBox()
        {
            InitializeComponent();
        }

        #endregion

        #region Events

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            _lastTime = DateTime.Now;
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            if (Opacity <= 0 && IsHandleCreated && !IsDisposed) Close();
            else if (Opacity > 0)
            {
                var elapsed = DateTime.Now - _lastTime;
                Opacity -= elapsed.TotalSeconds;
                _lastTime = DateTime.Now;
            }
        }

        #endregion

        #region Methods

        public static new void Show()
        {
            Show(null, "Operation complete");
        }

        public static void Show(string description)
        {
            Show(null, description);
        }

        public static void Show(IWin32Window owner, string description)
        {
            using (var form = new QuickTipBox() { Description = description, Opacity = 1 })
            {
                form.ShowDialog(owner);
            }
        }

        #endregion
    }
}
